﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickTools;
using System.Xml;
using System.Xml.Serialization;
using ManagedWinapi;
using ManagedWinapi.Accessibility;
using ManagedWinapi.Windows;
using System.Diagnostics;
using QuickTools.PluginUpdater;
using QuickTools.ProcessHelper;

namespace TestingSandbox
{
    class Program
    {
        static bool isMainWindow(SystemWindow w)
        {
            return w.Title == "QMainDialogClass";
        }
        static bool isAeqStatus(SystemWindow w)
        {
            return w.Title == "m_pLabelAeqStatus";
        }
        [STAThread]
        static void Main(string[] args)
        {
            QuickTools.ProcessHelper.ProcessModule module = new QuickTools.ProcessHelper.ProcessModule();
            SettingsWindow window = new SettingsWindow(module);
            window.ShowDialog();
            return;
            SystemWindow[] ToplevelWindows = SystemWindow.AllToplevelWindows;
            foreach (SystemWindow win in ToplevelWindows)
            {
                if (win.Title.StartsWith("ESL Wire"))
                {
                    SystemWindow[] candidates = win.FilterDescendantWindows(false, (SystemWindow w) => { return w.Title == "QMainDialogClass"; });
                    foreach (SystemWindow w in candidates)
                    {
                        SystemWindow[] candidates2 = w.FilterDescendantWindows(false, (SystemWindow w2) => { return w2.Title == "m_pGamesPage"; });
                        string[] path = new string[] { "m_pGamesPageWidget", "frame_2", "m_stackedWidget", "page_2", "m_pLabelAeqStatus" };
                        SystemWindow alt = GetWindowWithPath(candidates2[0], path, 0);
                        if (alt != null)
                        {
                            if (alt.Visible)
                                Console.WriteLine("MONITORING!");
                            else
                                Console.WriteLine("NOT MONITORING!");
                        }
                        else
                            Console.WriteLine("ERROR!");
                    }
                }
            }
            
            Console.ReadLine();
            /*WirePluginUpdater u = new WirePluginUpdater("file://C:/Users/Lucas/Desktop/quicktools/quickTools4Wire/MatchmediaHelper/update.xml", new Version(0, 1));
            u.Channel = "Stable";
            u.UpdateCheckComplete += new EventHandler<WirePluginUpdater.UpdateCheckEventArgs>(u_UpdateCheckComplete);*/
        }

        private static SystemWindow GetWindowWithPath(SystemWindow start, string[] path, int index)
        {
            if (index >= path.Length)
                return null;
            var childWindows = start.AllChildWindows;
            foreach (SystemWindow w in childWindows)
            {
                if (path[index] == w.Title)
                {
                    index++;
                    if (index == path.Length)
                        return start;
                    else
                        return GetWindowWithPath(w, path, index);
                }
            }
            return null;
        }

        private static SystemWindow GetWindowWithTitle(SystemWindow[] windows, string title)
        {
            SystemWindow w = null;
            foreach (SystemWindow control in windows)
            {
                if ( control.Title == title)
                    return control;
                w = GetWindowWithTitle(control.AllChildWindows, title);
                if (w != null)
                    return w;
            }
            return null;
        }

        static void u_UpdateCheckComplete(object sender, UpdateCheckEventArgs e)
        {
            WirePluginUpdater u = sender as WirePluginUpdater;
            if (e.HasError)
            {
                Console.WriteLine("Error: " + e.Error);
            }
            else
            {
                Update update = u.GetPreferredUpdate(e.Updates);
                if (update != null)
                {
                    update.DownloadAsync();
                }
            }
        }
    }
}
